package com.auxgroup.smarthome.job;

import com.auxgroup.smarthome.DatapointControlEnum;
import com.auxgroup.smarthome.appconst.BusinessConstant;
import com.auxgroup.smarthome.enterprise.bean.AppSleepDiyCacheInfo;
import com.auxgroup.smarthome.enterprise.service.DeviceManageService;
import com.auxgroup.smarthome.enterprise.service.ProductService;
import com.auxgroup.smarthome.redis.config.ObjectRedis;
import com.auxgroup.smarthome.repo.AppSleepDiyRedisRepo;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.common.LOG;
import com.auxgroup.smarthome.web.ApiResponse;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fju on 2017/8/28.
 * 睡眠DIY定时任务
 */
@Component
public class AppSleepDiyJob implements SimpleJob {
    @Autowired
    private AppSleepDiyRedisRepo appSleepDiyRedisRepo;

    @Autowired
    private ObjectRedis objectRedis;

    @Autowired
    private ProductService productService;

    @Autowired
    private DeviceManageService deviceManageService;

    @Override
    public void execute(ShardingContext shardingContext) {
        LOG.info(this, "[睡眠DIY定时任务]开始执行 分片:" + shardingContext.getShardingItem());
        List<AppSleepDiyCacheInfo> appSleepDiyCacheInfoStream = appSleepDiyRedisRepo.getPartDataList(
                shardingContext.getShardingItem(),AppSleepDiyCacheInfo.class);
        if (appSleepDiyCacheInfoStream.size() == 0) {
            LOG.info(this, "[睡眠DIY定时任务]暂无定时任务");
            return;
        }
        appSleepDiyCacheInfoStream.forEach(appSleepDiyCacheInfo -> {
            //规则生效条件下
            if (appSleepDiyCacheInfo.atTime()) {
                String productKey = appSleepDiyCacheInfo.getProductKey();
                String did = appSleepDiyCacheInfo.getDid();
                Map<String,String> map = new LinkedHashMap<>();
                map.put(DatapointControlEnum.SLEEP_DIY.getName(),"1");
                map.put(DatapointControlEnum.SLEEP_MODE.getName(),"0");//睡眠DIY开启 ，睡眠模式关闭
                String inputMode = "";
                if(appSleepDiyCacheInfo.getMode() == BusinessConstant.APP_SLEEP_DIY_HEATING.getValue()){
                    inputMode = "4";
                }else if(appSleepDiyCacheInfo.getMode() == BusinessConstant.APP_SLEEP_DIY_REFRIGERATION.getValue()){
                    inputMode = "1";
                }
                map.put(DatapointControlEnum.AIR_CON_FUNC.getName(),inputMode);
                ApiResponse apiResponse = deviceManageService.sleepDiyRemoteControl(productKey, did, inputMode, appSleepDiyCacheInfo.getMap());
                if(apiResponse.getCode().equals(Syscode.SC_OK.getCode())){
                    deviceManageService.commonRemoteControlDevice(productKey,did,map);
                    appSleepDiyRedisRepo.deleteCache(appSleepDiyCacheInfo.getSleepDiyId());
                }
            }
        });
        LOG.info(this, "[睡眠DIY定时任务]结束执行 分片:" + shardingContext.getShardingItem());
    }

}
